package com.hand.wfive;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.doris.media.picker.app.MediaPickerApp;
import com.doris.word.html.WordToHtml;
import com.hand.wfive.loginAndVip.LoginConfig;
import com.hand.wfive.util.FileUtils;
import com.hand.wfive.util.RxHttpManager;
import com.liulishuo.filedownloader.FileDownloader;
import com.moor.imkf.utils.YKFUtils;
import com.umeng.commonsdk.UMConfigure;

import org.litepal.LitePal;

import update.UpdateAppUtils;

@SuppressLint("SdCardPath")
public class App extends MediaPickerApp {

    private static final String TAG = App.class.getSimpleName();

    private static App context;
    private Typeface songTypeface;

    public static App getContext() {
        return context;
    }

    public App() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
//        ARouter.openLog();
//        ARouter.openDebug();
        ARouter.init(this);
        RxHttpManager.init(this);
        LitePal.initialize(this);
        UMConfigure.preInit(this, LoginConfig.UmengId, getString(R.string.channel));
// word转html缓存目录
        WordToHtml.setCacheTempPath(getCacheTempPath());
        // 客服初始化
        YKFUtils.init(this);
        //初始化下载器
        FileDownloader.setupOnApplicationOnCreate(this);
        UpdateAppUtils.init(context);
    }
    public String getCacheSignPath() {
            String path = "/data/data/" + context.getPackageName() + "/cache/sign";
            logD("getCacheSignPath(): ", path, FileUtils.createFolder(path));
            return path;
        }
    public String getCachePath() {
        String path = "/data/data/" + context.getPackageName() + "/cache";
        logD("getCachePath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getCacheTempPath() {
        String path = "/data/data/" + context.getPackageName() + "/cache/temp";
        logD("getCacheTempPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getDatabasePath() {
        String path = "/data/data/" + context.getPackageName() + "/databases";
        logD("getDatabasePath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getImgPath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + getText(R.string.app_name);
        logD("getImgPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getVideoPath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MOVIES).getAbsolutePath() + "/" + getText(R.string.app_name);
        logD("getVideoPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getLocationPath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES).getAbsolutePath();
        path = path.substring(0, path.lastIndexOf("/"));
        logD("getLocationPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getWechatImgPath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/WeiXin";
        logD("getWechatImgPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getWechatFilePath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/WeiXin";
        logD("getWechatFilePath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getDcimPath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).getAbsolutePath();
        logD("getDcimPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getAudioPath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MUSIC).getAbsolutePath() + "/" + getText(R.string.app_name);
        logD("getAudioPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getDownloadPath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + getText(R.string.app_name);
        logD("getDownloadPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getDocumentsPath() {
        String path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + "/" + getText(R.string.app_name);
        logD("getDocumentsPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public String getSystemScreenshotPath(int type) {
        String path = Environment.getExternalStoragePublicDirectory(
                        type == 1 ? Environment.DIRECTORY_DCIM : Environment.DIRECTORY_PICTURES)
                .getAbsolutePath() + "/Screenshots";
        logD("getSystemScreenshotPath(): ", path, FileUtils.createFolder(path));
        return path;
    }

    public void copyText(String content) {
        ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setPrimaryClip(ClipData.newPlainText("text", content));
        Toast.makeText(this, "复制成功~", Toast.LENGTH_SHORT).show();
    }

    public Typeface getSongTypeface() {
        if (songTypeface == null) {
            songTypeface = Typeface.createFromAsset(context.getAssets(), "simsun.ttc");
        }
        return songTypeface;
    }

    private void logD(String... msgs) {
        StringBuilder sb = new StringBuilder();
        if (msgs != null && msgs.length > 0) {
            for (String item : msgs) {
                sb.append(item);
            }
        }
        Log.d(TAG, sb.toString());
    }
}
